package com.yj.blog.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.yj.blog.admin.dao.SysUserRoleRelationDao;
import com.yj.blog.admin.service.SysUserCacheService;
import com.yj.blog.admin.service.SysUserService;
import com.yj.blog.common.service.RedisService;
import com.yj.blog.mapper.SysUserRoleRelationMapper;
import com.yj.blog.model.SysResource;
import com.yj.blog.model.SysUser;
import com.yj.blog.model.SysUserRoleRelation;
import com.yj.blog.model.SysUserRoleRelationExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户缓存操作Service实现类
 */
@Service
public class SysUserCacheServiceImpl implements SysUserCacheService {
    @Autowired
    private SysUserService userService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private SysUserRoleRelationMapper userRoleRelationMapper;
    @Autowired
    private SysUserRoleRelationDao userRoleRelationDao;
    @Value("${redis.database}")
    private String REDIS_DATABASE;
    @Value("${redis.expire.common}")
    private Long REDIS_EXPIRE;
    @Value("${redis.key.user}")
    private String REDIS_KEY_ADMIN;
    @Value("${redis.key.resourceList}")
    private String REDIS_KEY_RESOURCE_LIST;

    @Override
    public void delUser(Long userId) {
        SysUser user = userService.getItem(userId);
        if (user != null) {
            String key = REDIS_DATABASE + ":" + REDIS_KEY_ADMIN + ":" + user.getUsername();
            redisService.del(key);
        }
    }

    @Override
    public void delResourceList(Long userId) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_RESOURCE_LIST + ":" + userId;
        redisService.del(key);
    }

    @Override
    public void delResourceListByRole(Long roleId) {
        SysUserRoleRelationExample example = new SysUserRoleRelationExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        List<SysUserRoleRelation> relationList = userRoleRelationMapper.selectByExample(example);
        if (CollUtil.isNotEmpty(relationList)) {
            String keyPrefix = REDIS_DATABASE + ":" + REDIS_KEY_RESOURCE_LIST + ":";
            List<String> keys = relationList.stream().map(relation -> keyPrefix + relation.getUserId()).collect(Collectors.toList());
            redisService.del(keys);
        }
    }

    @Override
    public void delResourceListByRoleIds(List<Long> roleIds) {
        SysUserRoleRelationExample example = new SysUserRoleRelationExample();
        example.createCriteria().andRoleIdIn(roleIds);
        List<SysUserRoleRelation> relationList = userRoleRelationMapper.selectByExample(example);
        if (CollUtil.isNotEmpty(relationList)) {
            String keyPrefix = REDIS_DATABASE + ":" + REDIS_KEY_RESOURCE_LIST + ":";
            List<String> keys = relationList.stream().map(relation -> keyPrefix + relation.getUserId()).collect(Collectors.toList());
            redisService.del(keys);
        }
    }

    @Override
    public void delResourceListByResource(Long resourceId) {
        List<Long> userIdList = userRoleRelationDao.getUserIdList(resourceId);
        if (CollUtil.isNotEmpty(userIdList)) {
            String keyPrefix = REDIS_DATABASE + ":" + REDIS_KEY_RESOURCE_LIST + ":";
            List<String> keys = userIdList.stream().map(userId -> keyPrefix + userId).collect(Collectors.toList());
            redisService.del(keys);
        }
    }

    @Override
    public SysUser getUser(String username) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_ADMIN + ":" + username;
        return (SysUser) redisService.get(key);
    }

    @Override
    public void setUser(SysUser user) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_ADMIN + ":" + user.getUsername();
        redisService.set(key, user, REDIS_EXPIRE);
    }

    @Override
    public List<SysResource> getResourceList(Long userId) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_RESOURCE_LIST + ":" + userId;
        return (List<SysResource>) redisService.get(key);
    }

    @Override
    public void setResourceList(Long userId, List<SysResource> resourceList) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_RESOURCE_LIST + ":" + userId;
        redisService.set(key, resourceList, REDIS_EXPIRE);
    }
}
